﻿using System.Configuration;
using System.ServiceModel;
using System.Web.Configuration;

namespace VASJ.Provider
{
    public abstract class BaseHelper
    {
        protected BasicHttpBinding _binding;
        protected EndpointAddress _endpoint;

        protected string _ApplicationName = string.Empty;

        protected string username = string.Empty;
        protected string password = string.Empty;

        /// <summary>
        /// Init
        /// </summary>
        protected BaseHelper()
        {
            BaseHelper.GetConfig(out _binding, out _endpoint, out _ApplicationName, out username, out password);
        }

        /// <summary>
        /// GetService
        /// </summary>
        /// <returns></returns>
        public AcpInetService.AcpInetClient GetService()
        {
            AcpInetService.AcpInetClient acpiNet = new AcpInetService.AcpInetClient(_binding, _endpoint);
            acpiNet.ClientCredentials.UserName.UserName = this.username;
            acpiNet.ClientCredentials.UserName.Password = this.password;

            return acpiNet;
        }

        /// <summary>
        /// GetService
        /// </summary>
        /// <param name="bh"></param>
        /// <returns></returns>
        public static AcpInetService.AcpInetClient GetService(BaseHelper bh)
        {
            return bh.GetService();
        }

        /// <summary>
        /// GetConfig
        /// </summary>
        /// <param name="_binding"></param>
        /// <param name="_endpoint"></param>
        /// <param name="_applicationname"></param>
        /// <param name="_username"></param>
        /// <param name="_password"></param>
        public static void GetConfig(out BasicHttpBinding _binding, out EndpointAddress _endpoint, out string _applicationname, out string _username, out string _password)
        {
            MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");

            string defaultProvider = membershipSection.DefaultProvider;

            ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];

            string connectionStringName = providerSettings.Parameters["connectionStringName"];

            _applicationname = providerSettings.Parameters["applicationName"];
            _username = providerSettings.Parameters["username"];
            _password = providerSettings.Parameters["password"];

            string isHttps = providerSettings.Parameters["isHttps"];

            BasicHttpBinding binding = new BasicHttpBinding();

            binding.Name = "basicEndpoint";
            binding.CloseTimeout = System.TimeSpan.Parse("00:01:00");
            binding.OpenTimeout = System.TimeSpan.Parse("00:01:00");
            binding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
            binding.SendTimeout = System.TimeSpan.Parse("00:01:00");
            binding.AllowCookies = false;
            binding.BypassProxyOnLocal = false;
            binding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard;
            binding.MaxBufferSize = 2097152;
            binding.MaxBufferPoolSize = 524288;
            binding.MaxReceivedMessageSize = 2097152;
            binding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
            binding.TextEncoding = System.Text.Encoding.UTF8;
            binding.TransferMode = System.ServiceModel.TransferMode.Buffered;
            binding.UseDefaultWebProxy = true;
            binding.ReaderQuotas.MaxDepth = 32;
            binding.ReaderQuotas.MaxStringContentLength = 8192;
            binding.ReaderQuotas.MaxArrayLength = 16384;
            binding.ReaderQuotas.MaxBytesPerRead = 4096;
            binding.ReaderQuotas.MaxNameTableCharCount = 16384;

            if (!string.IsNullOrEmpty(isHttps) && isHttps.ToLower() == "true")
            {
                /* ************* Https **************** */
                binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
                binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                binding.Security.Transport.Realm = "";
                /* ********************************************************************* */

                /* ************* Https **************** */
                binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
                binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;

                System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
                {
                    return true;
                };
                /* ********************************************************************* */
            }
            else
            {
                /* ************* Http **************** */
                binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.None;
                binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
                binding.Security.Transport.Realm = "";
                /* ********************************************************************* */

                binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
                binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
            }

            EndpointAddress endpoint = new EndpointAddress(connectionStringName);

            _binding = binding;
            _endpoint = endpoint;
        }
    }
}